function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatTime2(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function showLoading() {
  console.log("showloading-----");
  wx.showToast({
    title: '请稍后',
    icon: 'loading',
    duration: 5000
  });
}
// 获取时间
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate + " " + [hour, minute, second].map(formatNumber).join(':');
  return currentdate;
}
function hideLoading() {
  wx.hideToast();
}

function showModal(text) {
  wx.showModal({
    title: '提示',
    content: text,
    showCancel: false,
    success: function (res) {
      if (res.confirm) {

      }
    }
  })
}

function showFailModal() {
  wx.showModal({
    title: '提示',
    content: '网络异常，请检查网络',
    success: function (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      }
    }
  })
}

function getProductNameById(id) {
  var products = wx.getStorageSync('products');
  for (var i = 0; i < products.length; i++) {
    var product = products[i];
    if (product.p_id == id) {
      return product.p_title;
    }
  }
}

function getProductById(id) {
  var products = wx.getStorageSync('products');
  for (var i = 0; i < products.length; i++) {
    var product = products[i];
    if (product.p_id == id) {
      return product;
    }
  }
}

function generateUUID() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  });
  return uuid;
}

function uuid(len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  var uuid = [], i;
  radix = radix || chars.length;

  if (len) {
    for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  } else {
    var r;

    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4';

    // Fill in random data.  At i==19 set the high bits of clock sequence as
    for (i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random() * 16;
        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
      }
    }
  }

  return uuid.join('');
}


function su() {

}
// 获取openid
function getOpenId(code) {
console.log("oooooo");
  console.log(serveUrl());
  wx.request({
    url: serveUrl() + "get_openid_by_code",
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: {
      code: code
    },
    success: function (res) {
      console.log("我是通过code获取");
      console.log(res.data.data)
      try {
        wx.setStorageSync('userStateAndOpenid', res.data.data)
      } catch (e) {
      }

      // return res.data.data.openid

    }
  })

}
 // 通过 status_code 获取 openid
function getOpenIdByStatus(status,code,that){
  console.log(status)
  if (status==null){
    wx.request({
      url: serveUrl() + "get_openid_by_code",
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      data: {
        code: code
      },
      success: function (res) {
        console.log("我是通过code获取");
        console.log(res.data.data)
        console.log(that.globalData)
        // that.globalData.status = res.data.data.status_code;
        // that.globalData.opeid = res.data.data.openid
        // 设置缓存
        try {
          wx.setStorageSync('userStateAndOpenid', res.data.data)
        } catch (e) {
        }

        // return res.data.data.openid

      }
    })
  }else{
    console.log("花花")
    console.log(status)
  wx.request({
    url: serveUrl() + "get_openid_by_status_code",
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: {
      status_code: status,
      code: code
    },
    success: function (res) {
      console.log("我是通过 status_code 获取 openid");
      console.log(res)
      // that.globalData.status = res.data.data.status_code;
      // that.globalData.opeid = res.data.data.openid
      try {
        wx.setStorageSync('userStateAndOpenid', res.data.data)
      } catch (e) {
      }
    }
  })
  }

 }
// 添加用户
function addUserInfos(userData,openids){
console.log("woyaotianjishd");
  console.log(userData)
  var aa = new Date()
  var loginTime = getNowFormatDate();
  console.log(loginTime)
  console.log(openids)
  var sex = (userData.gender == 1 ? "男" : "女");
  console.log(sex)
  wx.request({
    url: serveUrl() + "insert_user_data",
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    data: {
      head_img: userData.avatarUrl,
      openid: openids,
      nick_name: userData.nickName,
      last_login_time: loginTime,
      login_counts:1,
      gender: sex,
      province: userData.province,
      city:userData.city,
      country: userData.country
    },
        success: function (res) {
          console.log("我是添加用户的结果");
          console.log(res)
          // 设置缓存
          // try {
          //   wx.setStorageSync('userStateAndOpenid', res.data.data)
          // } catch (e) {
          // }
        }
  })
}
// 服务器地址
function serveUrl(){
  var serveUrl = 'https://www.logclub.com/xahh/1/index.php?method=';
  return serveUrl;
}
module.exports = {
  formatTime: formatTime,
  formatTime2: formatTime2,
  showLoading: showLoading,
  hideLoading: hideLoading,
  showFailModal: showFailModal,
  getProductNameById: getProductNameById,
  getProductById: getProductById,
  showModal: showModal,
  uuid: uuid,
  su: su,
  getNowFormatDate: getNowFormatDate,
  serveUrl: serveUrl(),
  getOpenId: getOpenId,
  addUserInfos: addUserInfos,
  getOpenIdByStatus: getOpenIdByStatus
}
